package com.sunshine.exception;

import com.sunshine.Service.impl.UserServiceImpl;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public ResponseEntity<String> handleSQLIntegrityConstraintViolationException(SQLIntegrityConstraintViolationException e) {
        return ResponseEntity.badRequest().body("注册失败，该账号已经存在");
    }
    
    @ExceptionHandler(DuplicateKeyException.class)
    public ResponseEntity<String> handleDuplicateKeyException(DuplicateKeyException e) {
        return ResponseEntity.badRequest().body("注册失败，该账号已经存在");
    }

    // 添加对登录异常的处理
    @ExceptionHandler(UserServiceImpl.LoginException.class)
    public ResponseEntity<String> handleLoginException(UserServiceImpl.LoginException e) {
        return ResponseEntity.badRequest().body(e.getMessage());
    }
}